var fs = require('fs')
var express = require('express')
let template = require('art-template');
var router = express.Router()
const jwt = require("jsonwebtoken")
var axios = require("axios");
const url=require('url');
const querystring=require('querystring')
router.get('/load',(req,res)=>{
    var arg = url.parse(req.url).query;
    var params = querystring.parse(arg);
    var Url = params.url; 
    res.render('front-stage/l-loading.html', {Url: Url})
})


router.get('/index', function (req, res) {
    var  hot,movie,tv,need; 
    if (req.session.token == undefined){
         // 遍历出来新上线电影
        axios({
            url: "video/index/newOnLine",
            method: "get",
        })
            .then(function (data) {
                movie = data.data.data; 
                 // 遍历出来热行榜
                axios({
                    url: "video/index/hotList",
                    method: "get",
                    headers: {
                        token: req.session.token ? req.session.token : ""
                    }
                })
                .then(function (data) {
                    hot = data.data.data;
                    // 遍历出来电视剧
                    axios({
                        url: "video/index/MoreTV",
                        method: "get",
                        headers: {
                            token: req.session.token ? req.session.token : ""
                        }
                    })
                    .then(function (data) {
                        tv = data.data.data;
                        // 遍历出来必看榜前八个
                        axios({
                            url: "video/index/mustSeeVideo",
                            method: "get",
                            headers: {
                                token: req.session.token ? req.session.token : ""
                            }
                        })
                        .then(function (data){
                            need=data.data.data
                            let obj ={
                                tv: tv,
                                movie:movie,
                                hot:hot,
                                need:need
                            }
   
                        axios({
                            url: "video/order/findAllVip",
                            method: "post",
                        })
                        .then(function (data) {
                            let allonevip = data.data.object 
                               res.render('front-stage/l-index.html',{userinfo:'',allonevip:allonevip,obj:obj})
             
                        })
                        .catch(function (error) {
                            console.log(error)
                        })

                        })
                        .catch(function (err) {
                           
                            console.log('失败')
                        })       
                    })
                    .catch(function (err) {
                      
                        console.log('失败')
                    }) 
                })
                .catch(function (err) {
                    console.log('失败')
                }) 
            })
            .catch(function (err) {
                console.log('失败')
            })  

    }else{
         // 遍历出来新上线电影
         axios({
            url: "video/index/newOnLine",
            method: "get",
        })
            .then(function (data) {
                movie = data.data.data; 
                 // 遍历出来热行榜
                axios({
                    url: "video/index/hotList",
                    method: "get",
                    headers: {
                        token: req.session.token ? req.session.token : ""
                    }
                })
                .then(function (data) {
                    hot = data.data.data;
                    // 遍历出来电视剧
                    axios({
                        url: "video/index/MoreTV",
                        method: "get",
                        headers: {
                            token: req.session.token ? req.session.token : ""
                        }
                    })
                    .then(function (data) {
                        console.log(122)
                        console.log(data.data.data)
                        tv = data.data.data;
                        // 遍历出来必看榜前八个
                        axios({
                            url: "video/index/mustSeeVideo",
                            method: "get",
                            headers: {
                                token: req.session.token ? req.session.token : ""
                            }
                        })
                        .then(function (data){ 
                            need=data.data.data
                            let obj ={
                                tv: tv,
                                movie:movie,
                                hot:hot,
                                need:need
                            }

                            axios({
                                url: "video/order/findAllVip",
                                method: "post",
                            })
                            .then(function (data) {
                                let allonevip = data.data.object 

                                axios({
                                    url: "video/user/findUserById",
                                    params: {
                                        userId: jwt.decode(req.session.token).userId,
                                    },
                                    method: "get",
                                })
                                    .then(function (data) {
                                        console.log(data.data.object)
                                        let viptype = data.data.object.vip;
                                        console.log(viptype);
                                        if (viptype == "0") {
                                            console.log("普通用户")
                                            res.render('front-stage/l-index.html',{userinfo:jwt.decode(req.session.token),allonevip:allonevip,obj:obj})
                                        } else if (viptype == "1") {
                                            console.log("vip用户")
                                            res.render('front-stage/l-index.html',{userinfo:jwt.decode(req.session.token),allonevip:allonevip,obj:obj})
                                        } else {
                                            console.log("管理员")
                                            res.render('front-stage/l-index.html', { userinfo: jwt.decode(req.session.token), allonevip: allonevip, obj: obj  })
                                        }
                                    })
                                    .catch(function (error) {
                                        console.log(error)
                                        res.status(500).json({
                                            message: '获取用户信息失败'
                                        })
                                    })


                               
                            })
                            .catch(function (error) {
                              console.log('失败') 
                            })

                        })
                        .catch(function (err) {
                           
                            console.log('失败')
                        })  
                    })
                    .catch(function (err) {
                        console.log('失败')
                    }) 
                
                })
                .catch(function (err) {
                    console.log('失败')
                }) 
            })
            .catch(function (err) {
                console.log('失败')
            }) 
    } 
})



router.get('/photo',(req,res)=>{
     // 遍历出来7张轮播图
     axios({        
        url: "video/index/indexRecommend",
        method: "get",
        headers: {
            token: req.session.token ? req.session.token : ""
        }
    }).then(function (data) {
        res.status(200).json({
            data: data.data.data
        })
    })
    .catch(function (err) {
        res.status(500).json({
            message: '添加失败'
        })
    })
})

// 电影
router.get('/movie/hotList', function (req, res) {
    console.log(req.query)
    axios({
        url: "video/index/hotList",
        method: "GET",
    })
        .then(function (data) {
            console.log(data.data)
            res.status(200).json({
                code: 200,
                data: data.data,
            })
        })
        .catch(function (error) {
            console.log(error)
            res.status(500).json({
            })
        })
})

// 飙升榜
router.get('/index/skyrocketVideo', function (req, res) {
    axios({
        url: "video/index/skyrocketVideo",
        method: "GET",
    })
    .then(function (data){
        res.status(200).json({ 
            data: data.data,
        })
    })
    .catch(function (error) {
        res.status(500).json({
            message:'错误'
        })
    })
})

// /*增加面试者！！！！ */
// router.post('/videoCRUD/addVideo', function (req, res) {
//     axios({
//         url: 'http://42.194.158.211:8080/video/videoCRUD/addVideo',
//         method: 'post',
//         headers: {
//             // token: req.session.token ? req.session.token : "",
//             "Content-Type": req.headers['content-type']  
//         },
//         maxContentLength: Infinity,
//         maxBodyLength: Infinity,
//         responseType: 'stream',
//         data: req
//     })
//         .then(response => {
//             // 将源链接的响应头转发给下游
//             for (const [key, value] of Object.entries(response.headers)) {
//                 res.append(key, value);
//             }
//             response.data.pipe(res);
//         }).catch(error => {
//             res.send('Error');
//             console.error('出错：', error);
//             console.error('响应：', error.response.data.read(65535).toString());
//         })
// });

module.exports = router